# accounts/forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from .models import User


class UserRegistrationForm(UserCreationForm):
    role = forms.ChoiceField(choices=User.ROLE_CHOICES, initial='customer')
    phone = forms.CharField(max_length=15, required=False)

    # 新增字段（根据角色显示）
    address = forms.CharField(
        label="地址", max_length=255, required=False,
        widget=forms.TextInput(attrs={'class': 'form-control'})
    )
    company = forms.CharField(
        label="公司名称", max_length=100, required=False,
        widget=forms.TextInput(attrs={'class': 'form-control'})
    )
    business_license = forms.CharField(
        label="营业执照编号", max_length=100, required=False,
        widget=forms.TextInput(attrs={'class': 'form-control'})
    )

    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2', 'role', 'phone']



class UserLoginForm(AuthenticationForm):
    username = forms.CharField(label='用户名', required=True)
    password = forms.CharField(label='密码', widget=forms.PasswordInput, required=True)



class TextEncryptionForm(forms.Form):
    plaintext = forms.CharField(
        label='请输入要加密的文本',
        widget=forms.Textarea(attrs={
            'rows': 5,
            'class': 'form-control',
            'placeholder': '输入敏感信息...'
        }),
        required=True
    )

from django import forms
from .models import AgriculturalProduct


class AgriculturalProductForm(forms.ModelForm):
    class Meta:
        model = AgriculturalProduct
        fields = [
            'product_name',  # ✅ 新增字段
            'raw_material_source',
            'production_process',
            'inspection_batch',
            'logistics_flow',
            'anti_counterfeiting_certification',
            'product_image',
            'price'
        ]
        widgets = {
            'product_name': forms.TextInput(attrs={'class': 'form-control'}),
            'raw_material_source': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
            'production_process': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
            'inspection_batch': forms.TextInput(attrs={'class': 'form-control'}),
            'logistics_flow': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
            'anti_counterfeiting_certification': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
            'product_image': forms.FileInput(attrs={'class': 'form-control'}),
            'price': forms.NumberInput(attrs={'class': 'form-control', 'step': '0.01'})
        }


from django import forms
from .models import User

class UserEditForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ['username', 'email', 'phone', 'first_name', 'last_name']

    def __init__(self, *args, **kwargs):
        super(UserEditForm, self).__init__(*args, **kwargs)
        placeholders = {
            'username': '请输入用户名',
            'email': '请输入邮箱地址',
            'phone': '请输入手机号码',
            'first_name': '请输入名字',
            'last_name': '请输入姓氏',
        }

        for field_name in self.fields:
            field = self.fields[field_name]
            field.widget.attrs.update({
                'class': 'form-control',
                'placeholder': placeholders.get(field_name, ''),
            })


